Welcome back. The basic syntax for defining a function is the word def,
D-E-F, its short for define.
Then you get any Python variable name and then you have an open and close parenthesis.
Inside the parentheses, we're going to see later that you could have
more variable names but we don't have any for this function and then we've got a colon.
Below that, you've got an indented block of code.
You've already seen indented blocks of code with the while loop, the for loop,
or if statements, they work the same way for function definitions.
All the lines that are indented by that same number of spaces they're
all part of the function definition for the hello function.
And when we get another line of code down here on line five,
that's outdented at the same level as def,
that's going to be the end of the function definition.
We have an optional comment on the first line of the function.
If it's included it's called a doc string and there are some tools in Python for
automatically generating documentation of
a program that'll show the docstrings that are associated with functions.
It's often a multi-line string,
that's why we use this triple quotes.
We could have some more docstring here.
The triple quotes lets you have a string that goes onto multiple lines.
Now when we execute just the code that we have here,
nothing is going to print out.
Even though there are two print statements on lines four and five,
they won't actually be executed.
All that happens from lines one through five is that the function object gets created.
That function doesn't get executed.
Lines four and five here are only going to execute if we invoke the function.
So, I'm going to have some code that's outdented at the same level as
the def and I'm going to invoke the function hello.
Now, when we execut it lines four and five will run.
And it says, hello glad to meet you.
Suppose I print something that says,
we are here and then another invocation of hello.
See if you can predict what we're going to get in the output window.
What we get is
the first two lines come from our first invocation of hello and then we get,
the we are here, and then we get
two more lines that come from the second invocation of hello.
Let's see that in codelens.
So, you can see that executing lines one through five just creates
a variable called hello whose value is a function object.
It doesn't execute that function object.
When I invoke the function on line seven it passes control to the function.
So, we then actually execute the contents that are inside of it.
So, we execute line four which prints out, hello,
we execute line five and now we're done so we're going to
resume back here after the execution of hello.
So, we get to line eight and we print, we are here,
and then we have another invocation that passes control to the function,
it does its stuff and then it passes control back after line nine,
and we're at the end of the function execution.
So, that's the basics of function definition.
The syntax, we start with the word def,
and then we have a function name, and then parentheses,
and then a colon, we've got an indented block of code for the contents of the function.
Executing the def statement,
that just creates the function it doesn't execute it and we need other code afterwards,
like we have on line seven.
When we do a function invocation it passes control to the code for the function.
So, those lines of code inside the function can execute it and then we
resume right after the spot where the function invocation happens.
See you next time when we add formal parameters.